package com.fbooks.Controller;



import com.alibaba.fastjson.JSONObject;
import com.fbooks.bean.Article;
import com.fbooks.bean.ArticleCatalog;
import com.fbooks.mapper.ArticleMapper;
import com.fbooks.service.ArticleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: LuoRunFeng
 * @Email: 475403147@qq.com
 * @Time: 2020/5/23 0023 10:33
 * @Team: 蜂鸟工作室
 */
@Controller
@CrossOrigin
@RequestMapping("article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;
    @Autowired
    private ArticleMapper articleMapper;


    @RequestMapping("all")
    @ResponseBody
    public PageInfo<Article> getArticleByPages(@RequestBody JSONObject jsonObject){
        String page = String.valueOf(jsonObject.get("page"));
        String size = String.valueOf(jsonObject.get("size"));
        PageInfo<Article> articles = articleService.getArticleByPages(page,size);
        return articles;
    }

    /**
     * 发布文章
     * @param article
     * @return
     */
    @RequestMapping("releaseArticle")
    @ResponseBody
    public JSONObject releaseArticle(@RequestBody Article article){
        System.out.println(article);
        JSONObject jsonObject = articleService.releaseArticle(article);
        return jsonObject;
    }

    /**
     * 获取文章
     * @param article
     * @return
     */
    @RequestMapping("selectArticleById")
    @ResponseBody
    public JSONObject selectArticleById(@RequestBody Article article){
        System.out.println(article.getId());
        JSONObject articleInfo = articleService.selectArticleById(article.getId());
        return articleInfo;
    }


    /**
     * 获取该用户名下的文章数
     * @param article
     * @return
     */
    @RequestMapping("getAllArticle")
    @ResponseBody
    public  List<Article> getAllArticle(@RequestBody Article article){

        System.out.println(article.getUserId());
        List<Article> allArticle = articleService.getAllArticle(article.getUserId());
        return allArticle;
    }

    /**
     * 删除文章
     * @param article
     * @return
     */
    @RequestMapping("deleteArticleById")
    @ResponseBody
    public JSONObject deleteArticleById(@RequestBody Article article){
        JSONObject jsonObject = articleService.deleteArticleById(article.getId());
        return jsonObject;
    }


    /**
     * 文章列表
     * @return
     */
    @RequestMapping("CatalogList")
    @ResponseBody
    public List<ArticleCatalog> getArticleCatalogList(){
        List<ArticleCatalog> articleCatalogList = articleService.getArticleCatalog();
        return articleCatalogList;
    }


    /**
     * 获取用户文章分类（分类下有文章的那种）
     * @param jsonObject  (包含用户id)
     * @return
     */
    @RequestMapping("getArticleCatalog")
    @ResponseBody
    public List<JSONObject>  getUserArticleCatalog(@RequestBody JSONObject jsonObject){
        String uid = (String) jsonObject.get("uid");
        List<JSONObject> userArticleCatalog = articleService.getUserArticleCatalog(uid);
        return userArticleCatalog;
    }

    /**
     * 获取某一个用户 某个分类下的文章
     * @param jsonObject
     * @return
     */
    @RequestMapping("getCatalogArticle")
    @ResponseBody
    public List<Article> getCatalogArticle(@RequestBody JSONObject jsonObject){
        String uid = (String) jsonObject.get("uid");
        String catalogId = (String) jsonObject.get("catalogId");
        List<Article> catalogArticle = articleService.getCatalogArticle(uid, catalogId);
        return catalogArticle;
    }
}
